package com.hu.zhcc.shiro.controller.lgx;

import com.hu.zhcc.shiro.entity.lgx.*;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssess;
import com.hu.zhcc.shiro.service.lgx.*;
import com.hu.zhcc.shiro.service.xy.TAssessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class TSetmealController {

    @Autowired
    TCheckitemService tCheckitemService;
    @Autowired
    private TAssessService assessService;
    @Autowired
    TClinicDepartmentService tClinicDepartmentService;
    @Autowired
    TCheckgroupService tCheckgroupService;
    @Autowired
    TSetmealService tSetmealService;
    @Autowired
    TCheckgroupCheckitemService tCheckgroupCheckitemService;
    @Autowired
    TSetmealCheckgroupService tSetmealCheckgroupService;
    @Autowired
    TMemberExaminationService tMemberExaminationService;
    @Autowired
    TBodyCkService tBodyCkService;
    @Autowired
    TAssessService tAssessService;
    //就诊科室
    @GetMapping("/tClinicDepartmentsList")
    public List<TClinicDepartment> tClinicDepartmentsList(){
        List<TClinicDepartment> tClinicDepartments = tClinicDepartmentService.tclinicDepartmentList();
        return tClinicDepartments;
    }
    //会员检查表显示
    @GetMapping("/tMemberExList")
    public List<TMemberExamination> tMemberExList(){
        List<TMemberExamination> tMemberExaminations = tMemberExaminationService.tmemberExList();
        return tMemberExaminations;
    }
    //检查表
    @GetMapping("/tCheckitemList")
    public List<TCheckitem> tCheckitemList(){
        List<TCheckitem> tCheckitems = tCheckitemService.tcheckitemList();
        return tCheckitems;
    }
    //检查表查询
    @PostMapping("/tCheckitemId")
    public List<TCheckitem> tCheckitemId(@RequestBody  Map<String, String> map) {
        List<TCheckitem> id = tCheckitemService.tcheckitemId(Integer.parseInt(map.get("id")));
        return id;
    }
    //修改套餐
    @PostMapping("/makeup")
    public int makeup(@RequestBody Make make){
        Date date=new Date();
        int i = tSetmealService.updateByPrimaryKey(make.getFroms());
        TCheckgroup id = tCheckgroupService.TCheckgroupid(make.getFroms().getId());
        for (Integer a:make.getLists()){
           TCheckgroupCheckitemKey key=new TCheckgroupCheckitemKey();
            key.setCheckgroupId(id.getId());
            key.setCheckitemId(a);
            int i1 = tCheckgroupCheckitemService.deleteByPrimaryKey(key);
        }
        for (Integer a:make.getLists()){
            TCheckgroupCheckitemKey key=new TCheckgroupCheckitemKey();
            key.setCheckgroupId(id.getId());
            key.setCheckitemId(a);
            int i1 = tCheckgroupCheckitemService.insertSelective(key);
        }
        return i;
    }
    //修改套餐状态
    @PostMapping("/tSetmealup")
    public int tSetmealup(@RequestBody TSetmeal tSetmeal){
        int i = tSetmealService.updateByPrimaryKey(tSetmeal);
        return i;
    }
    //添加套餐
    @PostMapping("/makeadd")
    public int  makeadd(@RequestBody Make make){
        make.getFroms().setStatus(0);
        int insert = tSetmealService.insert(make.getFroms());
        Integer id =make.getFroms().getId();
        TCheckgroup tCheckgroup=new TCheckgroup();
        tCheckgroup.setName(make.getFroms().getName());
        tCheckgroup.setCode(make.getFroms().getCode());
        tCheckgroup.setSex(make.getFroms().getSex());
        tCheckgroup.setRemark(make.getFroms().getRemark());
        tCheckgroup.setAttention(make.getFroms().getAttention());
        tCheckgroup.setCreatetime(make.getFroms().getCreatetime());
        tCheckgroup.setUpdatetime(make.getFroms().getUpdatetine());
        tCheckgroup.setHelpcode(make.getFroms().getHelpcode());
        int insert1 = tCheckgroupService.insert(tCheckgroup);
        int ids=tCheckgroup.getId();
        TSetmealCheckgroup tSetmealCheckgroup=new TSetmealCheckgroup();
        tSetmealCheckgroup.setSetmealId(id);
        tSetmealCheckgroup.setCheckgroupId(ids);
        System.out.println(tSetmealCheckgroup);
        int insert2 = tSetmealCheckgroupService.insert(tSetmealCheckgroup);
        for (Integer a:make.getLists()){
            TCheckgroupCheckitemKey key=new TCheckgroupCheckitemKey();
            key.setCheckgroupId(ids);
            key.setCheckitemId(a);
            int i1 = tCheckgroupCheckitemService.insertSelective(key);
        }
        return insert;
    }
    //检查项修改
    @PostMapping("/tcheckitemup")
    public int tcheckitemup(@RequestBody TCheckitem tCheckitem){
        int i = tCheckitemService.updateByPrimaryKey(tCheckitem);
        return i;
    }
    //检查项添加
    @PostMapping("/tcheckitemadd")
    public int tcheckitemadd(@RequestBody TCheckitem tCheckitem){
        System.out.println(tCheckitem);
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("YYYY-MM-dd");
        String format1 = format.format(date);
        tCheckitem.setCreatetime(format1);
        tCheckitem.setUpdatetime(format1);
        int i = tCheckitemService.insert(tCheckitem);
        return i;
    }
    //查询检查项
    @PostMapping("/tCheckitems")
    public List<TCheckitem> tCheckitems(@RequestBody Map<String, String> map){
        List<TCheckitem> tCheckitems = tCheckitemService.tcheckitemId(Integer.parseInt(map.get("id")));
        return tCheckitems;
    }
    //会员检查表查询
    @PostMapping("/tMemberExamination")
    public TMemberExamination tMemberExamination(@RequestBody Map<String, String> map){
        TMemberExamination tMemberExamination = tMemberExaminationService.tmemberExid(Integer.parseInt(map.get("id")));
        return tMemberExamination;
    }
    //添加体检结果表
    @PostMapping("/tbodyckadd")
    public int tbodyckadd(@RequestBody List<TBodyCk> ton){
        int i=0;
        int b=0;
        for (TBodyCk t:ton){
            i = tBodyCkService.insert(t);
            b = t.getTMemberExaminationId();
        }
        TMemberExamination key = tMemberExaminationService.selectByPrimaryKey(b);
        TAssess tAssess1 = assessService.selectMember(key.getTMemberId());
        if (tAssess1!=null){
            TAssess tAssess=new TAssess();
            tAssess.setId(tAssess1.getId());
            tAssess.setMemberId(tAssess1.getMemberId());
            tAssess.setOperator(tAssess1.getOperator());
            tAssess.setTime(tAssess1.getTime());
            tAssess.setState(1);
            tAssess.setEvaluateId(tAssess1.getEvaluateId());
            int i1 = assessService.updateByPrimaryKeySelective(tAssess);
        }
        return i;
    }
    //会员检查表修改
    @PostMapping("/tMemberExup")
    public int tMemberExup(@RequestBody TMemberExamination tMemberExamination){
        int insert = tMemberExaminationService.updateByPrimaryKey(tMemberExamination);
        return insert;
    }
    //体检结果查询
    @PostMapping("/tbodyckid")
    public List<TBodyCx> tbodyckid(@RequestBody Map<String, String> map){
        List<TBodyCk> id = tBodyCkService.tbodyckid(Integer.parseInt(map.get("id")));
        List<TBodyCx> list=new ArrayList<>();
        for (TBodyCk tBodyCk : id) {
            TCheckitem tCheckitem = tCheckitemService.selectByPrimaryKey(tBodyCk.getTCheckitemId());
            TBodyCx tBodyCx=new TBodyCx();
            tBodyCx.setName(tCheckitem.getName());
            tBodyCx.setJg(tBodyCk.getJg());
            tBodyCx.setMaxck(tCheckitem.getMaxck());
            tBodyCx.setMinck(tCheckitem.getMinck());
            list.add(tBodyCx);
        }
        return list;
    }
}
